﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Text.RegularExpressions;

public partial class Sales_SaleSub : System.Web.UI.Page
{
    static int typeCount, total,countamount;
    static string[] lType;
    static TextBox txt;
    static string nodename;
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection sqlcon = null;
        SqlCommand sqlcmd1 = null, sqlcmd2 = null, sqlcmd3 = null,sqlcmd4=null,sqlcmd5=null;
        string cmd1 = "", cmd2 = "", cmd3 = "",cmd4="",cmd5="";
        
        SqlDataReader rd;
        sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["testLottery"].ConnectionString);
        sqlcon.Open();
        if (!IsPostBack)
        {
            cmd5 = "select id from Node where member= " + Session["uid"] + ";";
            sqlcmd5 = new SqlCommand(cmd5, sqlcon);
            rd = sqlcmd5.ExecuteReader();
            if (rd.Read())
            {
                nodename = rd[0].ToString();
            }
            rd.Close();
            cmd1 = "select count(*) from Lottery;";
            cmd4 = "select count(*) from Sales;"; 
            cmd2 = "select ltype from Node where member="+ Session["uid"] + ";";
            cmd3 = "select amount from Sales where node ='" + nodename + "' and date = '" + DateTime.Today.ToShortDateString() + "';";
            sqlcmd1 = new SqlCommand(cmd1, sqlcon);
            sqlcmd2 = new SqlCommand(cmd2, sqlcon);
            sqlcmd3 = new SqlCommand(cmd3, sqlcon);
            sqlcmd4 = new SqlCommand(cmd4, sqlcon);
            rd = sqlcmd1.ExecuteReader();
            if (rd.Read())
            {
                total = Int32.Parse(rd[0].ToString());
            }
            rd.Close();
            rd = sqlcmd4.ExecuteReader();
            if (rd.Read()) {
                countamount = Int32.Parse(rd[0].ToString());
            }
            rd.Close();
            rd = sqlcmd2.ExecuteReader();
            if (rd.Read())
            {
                string lotteryType = rd[0].ToString();
                lType = Regex.Split(lotteryType, ";", RegexOptions.IgnoreCase);
                typeCount = lType.Length;
            }
            rd.Close();
            for (int a = 0; a < total; a++)
                GridView1.Rows[a].Visible = false;

            for (int a = 0; a < typeCount; a++)
            {
                GridView1.Rows[Int32.Parse(lType[a]) - 1].Visible = true;
            }

            rd = sqlcmd3.ExecuteReader();
            if (rd.Read())
            {
                for (int a = 0; a < typeCount; a++)
                {
                    GridView1.Rows[Int32.Parse(lType[a]) - 1].Cells[2].Text = DateTime.Today.ToShortDateString() + "已提交";
                }
                Button1.Visible = false;
                Button2.Visible = false;
            }
            rd.Close();

        }
        sqlcon.Close();


    }
     protected void Button1_Click(object sender, EventArgs e)
    {
        if (Session["uid"] != null)
        {
            SqlConnection sqlcon = null;
            string cmd = "";
            sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["testLottery"].ConnectionString); //sqlcon = new SqlConnection(constring);
            sqlcon.Open();
            for (int a = 0; a < typeCount; a++)
            {
                txt = (TextBox)GridView1.Rows[Int32.Parse(lType[a]) - 1].Cells[2].FindControl("TextBox3");
                if (txt.Text == "")
                    txt.Text = "0";
            }
            for (int a = 0; a < typeCount; a++)
            {
                txt = (TextBox)GridView1.Rows[Int32.Parse(lType[a]) - 1].Cells[2].FindControl("TextBox3");
                if (RegConfirm(txt.Text) == 0)
                {
                    Label2.Visible = true;
                    break;
                }
                Label2.Visible = false;
            }
            if (Label2.Visible != true)
            {
                for (int a = 0; a < typeCount; a++)
                {
                    SqlCommand sqlcmd = null;
                    txt = (TextBox)GridView1.Rows[Int32.Parse(lType[a]) - 1].Cells[2].FindControl("TextBox3");
                    int amount;
                    if (RegConfirm(txt.Text) == 0)
                        txt.Text = "0";
                    amount = Int32.Parse(txt.Text);
                    GridView1.Rows[Int32.Parse(lType[a]) - 1].Cells[2].Text = DateTime.Today.ToShortDateString() + "已提交";
                    cmd = "insert into Sales(id,node,date,amount,lottery) values(" + (++countamount) + ",'" + nodename + "','" + DateTime.Today.ToShortDateString() + "'," + amount + "," + lType[a] + ");";
                    sqlcmd = new SqlCommand(cmd, sqlcon);
                    sqlcmd.ExecuteNonQuery();
                }
                Button1.Visible = false;
                Button2.Visible = false;
            }
        }
        else
            Label2.Text = "请重新登陆";
    }

     protected void Button2_Click(object sender, EventArgs e)
     {
         if (Session["uid"] != null)
         {
             for (int a = 0; a < typeCount; a++)
             {
                 SqlConnection sqlcon = null;
                 string cmd = "";
                 sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["testLottery"].ConnectionString); //sqlcon = new SqlConnection(constring);
                 sqlcon.Open();
                 SqlCommand sqlcmd = null;
                 txt = (TextBox)GridView1.Rows[Int32.Parse(lType[a]) - 1].Cells[2].FindControl("TextBox3");
                 int amount;
                 if (RegConfirm(txt.Text) == 0)
                     txt.Text = "0";
                 amount = Int32.Parse(txt.Text);
                 GridView1.Rows[Int32.Parse(lType[a]) - 1].Cells[2].Text = DateTime.Today.ToShortDateString() + "已提交";
                 cmd = "insert into Sales(id,node,date,amount,lottery) values(" + (++countamount) + ",'n" + Session["uid"] + "','" + DateTime.Today.ToShortDateString() + "',NULL," + lType[a] + ");";
                 sqlcmd = new SqlCommand(cmd, sqlcon);
                 sqlcmd.ExecuteNonQuery();
             }
             Button1.Visible = false;
             Button2.Visible = false;
         }
         else
             Label2.Text = "请重新登陆";
     }

    private int RegConfirm(string str)
    {
        String TextRegex = "^[0-9]+$";
        if ((Regex.IsMatch(str, TextRegex))&&str.Length<=7)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
}